package com.yxtbd.travel.travelvm.ui.login

import android.os.Build
import android.view.View
import androidx.annotation.RequiresApi
import androidx.lifecycle.Observer
import com.yxtbd.travel.travelvm.R
import com.yxtbd.travel.travelvm.base.BaseVMActivity
import com.yxtbd.travel.travelvm.ext.toast
import kotlinx.android.synthetic.main.activity_login.*
import kotlinx.android.synthetic.main.title_layout.*

/**
 * description ： 登录
 *
 * author : taodq
 * date : 2020/3/31 9:23
 */
class LoginActivity : BaseVMActivity<LoginViewModel>() {

    override fun providerVMClass(): Class<LoginViewModel> = LoginViewModel::class.java

    override fun getLayoutResId(): Int = R.layout.activity_login

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun initView() {
        mToolbar.title = "登录"
        button.setOnClickListener(mListener)
    }

    private val mListener = View.OnClickListener {
        when (it.id) {
            R.id.button -> {
                login()
            }
        }
    }

    override fun startObserve() {
        mViewModel?.apply {
            uiState.observe(this@LoginActivity, Observer { it ->
                progressBar.dismiss()
                it.showSuccess?.let {
                    toast(it.access_token)
                }
                it.showError?.let {
                    toast(it)
                }
            })
        }
    }

    private fun login() {
        progressBar.showDialog()
        mViewModel.login(username = userName.text.toString(), password = editText2.text.toString())
    }
}